home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / System / SafeSwitch / SafeSwitch.s < prev    next >
Text File  |  2000-05-26  |  13KB  |  360 lines

  1.  
  2.               include   "exec/memory.i"
  3.               include   "exec/semaphores.i"
  4.               include   "equs"              ; not included
  5.               include   "start.i"
  6.  
  7.  STRUCTURE  mymsg,LN_SIZE
  8.     APTR    ReplyPort
  9.     UWORD   Length
  10.     LONG    Reg1        ; d0
  11.     LONG    Reg2        ; a0
  12.     LONG    Reg3        ; a1
  13.     LONG    Reg4        ; a6
  14.     APTR    FuncPtr
  15.     LABEL   mymsg_SIZEOF
  16.  
  17.  
  18. init          move.l    4,a6                
  19.  
  20.               lea       intuiname,a1        ; open intuition
  21.               move.l    #37,d0
  22.               jsr       _LVOOpenLibrary(a6)
  23.               move.l    d0,d7               ; d7 = intuibase
  24.               beq       cleanup
  25.  
  26.               jsr       _LVOCreateMsgPort(a6)
  27.               move.l    d0,msgport
  28.               beq       cleanup
  29.  
  30.               move.l    d0,a0
  31.               move.b    MP_SIGBIT(a0),d1
  32.               move.l    #0,d0
  33.               bset      d1,d0
  34.               move.l    d0,d6               ; d6 = portsig
  35.  
  36.               lea       semaphore,a0        ; initialize semaphore
  37.               jsr       _LVOInitSemaphore(a6)
  38.  
  39.               lea       functab,a5          ; patch the routines
  40. setfuncloop   move.l    d7,a1               ; library base
  41.               move.l    (a5)+,a0            ; LVO
  42.               move.l    (a5)+,d0            ; our routine
  43.               jsr       _LVOSetFunction(a6)
  44.               move.l    (a5)+,a0            ; where to put oldFunc
  45.               move.l    d0,(a0)
  46.  
  47.               tst.l     (a5)                ; last routine to be replaced?
  48.               bne       setfuncloop
  49.  
  50. sigloop       move.l    d6,d0               ; wait for portsig
  51.               bset      #SIGBREAKB_CTRL_C,d0 ; and Ctrl+C
  52.               jsr       _LVOWait(a6)
  53.  
  54.               btst      #SIGBREAKB_CTRL_C,d0
  55.               bne       goaway
  56.  
  57.               lea       semaphore,a0
  58.               jsr       _LVOObtainSemaphore(a6)
  59.  
  60. msgloop       move.l    msgport,a0
  61.               jsr       _LVOGetMsg(a6)
  62.               tst.l     d0
  63.               beq       endmsgloop
  64.  
  65.               move.l    d0,a2
  66.               lea       Reg1(a2),a3
  67.               movem.l   (a3)+,d0/a0-a1/a6   ; ScreenDepth() needs 3 args + a6,
  68.               move.l    FuncPtr(a2),a3      ; others 0 or 1 args + a6
  69.               jsr       (a3)
  70.  
  71.               move.l    4,a6
  72.               move.l    a2,a1
  73.               jsr       _LVOFreeVec(a6)
  74.               bra       msgloop
  75.  
  76. endmsgloop    lea       semaphore,a0
  77.               jsr       _LVOReleaseSemaphore(a6)
  78.  
  79.               bra       sigloop
  80.  
  81. goaway        lea       functab,a5
  82. 10$           move.l    d7,a1               ; put the old routines back
  83.               move.l    (a5)+,a0            ; LVO
  84.               tst.l     (a5)+               ; skip our routine
  85.               move.l    (a5)+,a2            ; old routine
  86.               move.l    (a2),d0
  87.               jsr       _LVOSetFunction(a6)
  88.  
  89.               tst.l     (a5)                ; last routine?
  90.               bne       10$
  91.  
  92. cleanup       move.l    msgport,d2          ; delete msgport
  93.               beq       30$
  94. 10$           move.l    d2,a0               ; get all messages and free them
  95.               jsr       _LVOGetMsg(a6)      ; (even though we aren't supposed
  96.               tst.l     d0                  ; to be quitted =)
  97.               beq       20$
  98.               move.l    d0,a1
  99.               jsr       _LVOFreeVec(a6)
  100.               bra       10$
  101. 20$           move.l    d2,a0
  102.               jsr       _LVODeleteMsgPort(a6)
  103.  
  104. 30$           move.l    d7,d0               ; close intuition
  105.               beq       40$
  106.               move.l    d0,a1
  107.               jsr       _LVOCloseLibrary(a6)
  108.  
  109. 40$           move.l    #0,d0
  110.               rts
  111.  
  112.  
  113. ;/// "replacement functions"
  114.  
  115. ; ScreenToFront()
  116. func1         movem.l   d0/a0-a1/a6,-(a7)   ; preserve registers
  117.               move.l    4,a6                
  118.               lea       semaphore,a0
  119.               jsr       _LVOAttemptSemaphore(a6)
  120.               tst.l     d0                  ; did we get the semaphore?
  121.               beq       20$                 ; no -> switch screens later, return now
  122.               movem.l   (a7)+,d0/a0-a1/a6   ; yes -> restore registers
  123.               pea       10$                 ; where to go after oldFunc()
  124.               move.l    of1,-(a7)           ; go to oldFunc() after rts
  125.               rts
  126. 10$           movem.l   a0/a6,-(a7)         ; release the semaphore after oldFunc()
  127.               move.l    4,a6
  128.               lea       semaphore,a0        
  129.               jsr       _LVOReleaseSemaphore(a6)
  130.               movem.l   (a7)+,a0/a6
  131.               rts                           ; and return to the calling program
  132. 20$           move.l    #mymsg_SIZEOF,d0    ; send message: switch screens sometime
  133.               move.l    #MEMF_CLEAR ! MEMF_PUBLIC,d1
  134.               jsr       _LVOAllocVec(a6)    ; allocate memory for the message
  135.               tst.l     d0
  136.               beq       30$
  137.               move.l    d0,a1               ; initialize the message
  138.               move.w    #mymsg_SIZEOF,Length(a1)
  139.               move.l    of1,FuncPtr(a1)     ; call this function
  140.               move.l    (a7),Reg1(a1)       ; with these parameters
  141.               move.l    4(a7),Reg2(a1)
  142.               move.l    8(a7),Reg3(a1)
  143.               move.l    12(a7),Reg4(a1)
  144.               move.l    msgport,a0
  145.               jsr       _LVOPutMsg(a6)      ; whenever :)
  146. 30$           movem.l   (a7)+,d0/a0-a1/a6   ; restore registers
  147.               rts                           ; return to the calling program
  148.  
  149.  
  150. ; ScreenToBack() - same as ScreenToFront()
  151. func2         movem.l   d0/a0-a1/a6,-(a7)   
  152.               move.l    4,a6
  153.               lea       semaphore,a0
  154.               jsr       _LVOAttemptSemaphore(a6)
  155.               tst.l     d0
  156.               beq       20$
  157.               movem.l   (a7)+,d0/a0-a1/a6
  158.               pea       10$
  159.               move.l    of2,-(a7)
  160.               rts
  161. 10$           movem.l   a0/a6,-(a7)
  162.               move.l    4,a6
  163.               lea       semaphore,a0
  164.               jsr       _LVOReleaseSemaphore(a6)
  165.               movem.l   (a7)+,a0/a6
  166.               rts
  167. 20$           move.l    #mymsg_SIZEOF,d0
  168.               move.l    #MEMF_CLEAR ! MEMF_PUBLIC,d1
  169.               jsr       _LVOAllocVec(a6)
  170.               tst.l     d0
  171.               beq       30$
  172.               move.l    d0,a1
  173.               move.w    #mymsg_SIZEOF,Length(a1)
  174.               move.l    of2,FuncPtr(a1)
  175.               move.l    (a7),Reg1(a1)
  176.               move.l    4(a7),Reg2(a1)
  177.               move.l    8(a7),Reg3(a1)
  178.               move.l    12(a7),Reg4(a1)
  179.               move.l    msgport,a0
  180.               jsr       _LVOPutMsg(a6)
  181. 30$           movem.l   (a7)+,d0/a0-a1/a6
  182.               rts
  183.  
  184.  
  185. ; WBenchToFront() - same as ScreenToFront(), just sets a TRUE return code
  186. ; if AttemptSemaphore() failed (see second last line)
  187. func3         movem.l   d0/a0-a1/a6,-(a7)   
  188.               move.l    4,a6
  189.               lea       semaphore,a0
  190.               jsr       _LVOAttemptSemaphore(a6)
  191.               tst.l     d0
  192.               beq       20$
  193.               movem.l   (a7)+,d0/a0-a1/a6
  194.               pea       10$
  195.               move.l    of3,-(a7)
  196.               rts
  197. 10$           movem.l   a0/a6,-(a7)
  198.               move.l    4,a6
  199.               lea       semaphore,a0
  200.               jsr       _LVOReleaseSemaphore(a6)
  201.               movem.l   (a7)+,a0/a6
  202.               rts
  203. 20$           move.l    #mymsg_SIZEOF,d0
  204.               move.l    #MEMF_CLEAR ! MEMF_PUBLIC,d1
  205.               jsr       _LVOAllocVec(a6)
  206.               tst.l     d0
  207.               beq       30$
  208.               move.l    d0,a1
  209.               move.w    #mymsg_SIZEOF,Length(a1)
  210.               move.l    of3,FuncPtr(a1)
  211.               move.l    (a7),Reg1(a1)
  212.               move.l    4(a7),Reg2(a1)
  213.               move.l    8(a7),Reg3(a1)
  214.               move.l    12(a7),Reg4(a1)
  215.               move.l    msgport,a0
  216.               jsr       _LVOPutMsg(a6)
  217. 30$           movem.l   (a7)+,d0/a0-a1/a6
  218.               move.l    #1,d0               ; set return value: true
  219.               rts
  220.  
  221.  
  222. ; WBenchToBack() - same as WBenchToFront()
  223. func4         movem.l   d0/a0-a1/a6,-(a7)   
  224.               move.l    4,a6
  225.               lea       semaphore,a0
  226.               jsr       _LVOAttemptSemaphore(a6)
  227.               tst.l     d0
  228.               beq       20$
  229.               movem.l   (a7)+,d0/a0-a1/a6
  230.               pea       10$
  231.               move.l    of4,-(a7)
  232.               rts
  233. 10$           movem.l   a0/a6,-(a7)
  234.               move.l    4,a6
  235.               lea       semaphore,a0
  236.               jsr       _LVOReleaseSemaphore(a6)
  237.               movem.l   (a7)+,a0/a6
  238.               rts
  239. 20$           move.l    #mymsg_SIZEOF,d0
  240.               move.l    #MEMF_CLEAR ! MEMF_PUBLIC,d1
  241.               jsr       _LVOAllocVec(a6)
  242.               tst.l     d0
  243.               beq       30$
  244.               move.l    d0,a1
  245.               move.w    #mymsg_SIZEOF,Length(a1)
  246.               move.l    of4,FuncPtr(a1)
  247.               move.l    (a7),Reg1(a1)
  248.               move.l    4(a7),Reg2(a1)
  249.               move.l    8(a7),Reg3(a1)
  250.               move.l    12(a7),Reg4(a1)
  251.               move.l    msgport,a0
  252.               jsr       _LVOPutMsg(a6)
  253. 30$           movem.l   (a7)+,d0/a0-a1/a6
  254.               move.l    #1,d0
  255.               rts
  256.  
  257.  
  258. ; ScreenDepth() - same as ScreenToFront()
  259. func5         movem.l   d0/a0-a1/a6,-(a7)   
  260.               move.l    4,a6
  261.               lea       semaphore,a0
  262.               jsr       _LVOAttemptSemaphore(a6)
  263.               tst.l     d0
  264.               beq       20$
  265.               movem.l   (a7)+,d0/a0-a1/a6
  266.               pea       10$
  267.               move.l    of5,-(a7)
  268.               rts
  269. 10$           movem.l   a0/a6,-(a7)
  270.               move.l    4,a6
  271.               lea       semaphore,a0
  272.               jsr       _LVOReleaseSemaphore(a6)
  273.               movem.l   (a7)+,a0/a6
  274.               rts
  275. 20$           move.l    #mymsg_SIZEOF,d0
  276.               move.l    #MEMF_CLEAR ! MEMF_PUBLIC,d1
  277.               jsr       _LVOAllocVec(a6)
  278.               tst.l     d0
  279.               beq       30$
  280.               move.l    d0,a1
  281.               move.w    #mymsg_SIZEOF,Length(a1)
  282.               move.l    of5,FuncPtr(a1)
  283.               move.l    (a7),Reg1(a1)
  284.               move.l    4(a7),Reg2(a1)
  285.               move.l    8(a7),Reg3(a1)
  286.               move.l    12(a7),Reg4(a1)
  287.               move.l    msgport,a0
  288.               jsr       _LVOPutMsg(a6)
  289. 30$           movem.l   (a7)+,d0/a0-a1/a6
  290.               rts
  291.  
  292.  
  293.  
  294. ; OpenWindow()
  295. func6         movem.l   a0/a6,-(a7)         ; preserve the registers we change
  296.               move.l    4,a6                
  297.               lea       semaphore,a0        ; ObtainSemaphore() preserves all registers!
  298.               jsr       _LVOObtainSemaphore(a6)
  299.               movem.l   (a7)+,a0/a6
  300.               pea       10$                 ; where to go after oldFunc()
  301.               move.l    of6,-(a7)           ; go to oldFunc() after rts
  302.               rts
  303. 10$           movem.l   a0/a6,-(a7)         ; release the semaphore after oldFunc()
  304.               move.l    4,a6
  305.               lea       semaphore,a0
  306.               jsr       _LVOReleaseSemaphore(a6)
  307.               movem.l   (a7)+,a0/a6
  308.               rts                           ; and return to the calling program
  309.  
  310.  
  311. ; OpenWindowTagList() - same as OpenWindow()
  312. func7         movem.l   a0/a6,-(a7)         
  313.               move.l    4,a6
  314.               lea       semaphore,a0
  315.               jsr       _LVOObtainSemaphore(a6)
  316.               movem.l   (a7)+,a0/a6
  317.               pea       10$
  318.               move.l    of7,-(a7)
  319.               rts
  320. 10$           movem.l   a0/a6,-(a7)
  321.               move.l    4,a6
  322.               lea       semaphore,a0
  323.               jsr       _LVOReleaseSemaphore(a6)
  324.               movem.l   (a7)+,a0/a6
  325.               rts
  326.  
  327.  
  328. ;///
  329.  
  330.  
  331. intuiname     dc.b      'intuition.library',0
  332. verstr        dc.b      '$VER: SafeSwitch 1.0 (27 May 2000)',0
  333.               ds.l      0
  334.  
  335. functab       dc.l      _LVOScreenToFront,     func1, of1
  336.               dc.l      _LVOScreenToBack,      func2, of2
  337.               dc.l      _LVOWBenchToFront,     func3, of3
  338.               dc.l      _LVOWBenchToBack,      func4, of4
  339.               dc.l      _LVOScreenDepth,       func5, of5
  340.               dc.l      _LVOOpenWindow,        func6, of6
  341.               dc.l      _LVOOpenWindowTagList, func7, of7
  342.               dc.l      0
  343.  
  344.  
  345.               section   variables,data
  346.  
  347. of1           dc.l      0                   ; pointers to the old functions
  348. of2           dc.l      0
  349. of3           dc.l      0
  350. of4           dc.l      0
  351. of5           dc.l      0
  352. of6           dc.l      0
  353. of7           dc.l      0
  354.  
  355. msgport       dc.l      0
  356.  
  357. semaphore     ds.b      SS_SIZE
  358.  
  359.  
  360.